import ctime from '@/components/changeTime.vue'
import {
  share,
  AddMobileContactData
} from '@/utils/common-api.js'

import {
  mapActions
} from 'vuex'
// import posterShare from '@/pages_common/pages_me/singleGoodsPosters/singleGoodsPosters.vue'
export default {
  components: {
    ctime,
    // posterShare
  },
  data() {
    return {
      titleList: [{
        name: '全部'
      }, {
        name: '出售中'
      }, {
        name: '已下架'
      }, {
        name: '已过期'
      }],
      current: 0, // tabs组件的current值，表示当前活动的tab选项
      swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
      tabs: [{}, {}, {}, {}],
      pageList: [],
      show: false,
      stateText: '分类', //选择后的分类名称
      stateTextid: 0, //选择后的分类id
      allstattext: [{
        name: '全部',
        id: 0
      }, {
        name: '渠道-对应商家端',
        id: 1
      }, {
        name: '零售-对应消费端',
        id: 2
      }, {
        name: '对应商家端和消费端',
        id: 3
      }],
      pageindex: null, //当前页
      pagecont: 0, //总条数

      showXiajia: false, //下架
      showXiajiaId: 0, //下架id
      showShangjiaId: 0, //上架id
      //上架日期
      showsjrq: false,
      validPeriod: '', //有效期请选择
      Production: new Date().toISOString().slice(0, 10),
      modesc: 'time',
      paramssc: {
        year: true,
        month: true,
        day: true,
      },
      changqili: [{
        name: '长期有效',
        check: false,
        vlue: '2029-1-1'
      }, {
        name: '自定义时间',
        check: true
      }],
      haibaotrue: false, //
      defaultMapTitle: '暂无车源~快去发布吧',
      showPage: false,
      pagelistull: false
    }
  },
  onShow() {
    this.GetMyGoodsListDataList(0) // 列表
    this.getPosters() //单品海报
  },
  created() {
    // console.log('1')
    // this.GetMyGoodsListDataList(0) // 列表
  },
  methods: {
    ...mapActions(['setGoodsData',
      'setPosterBgTemplate'
    ]), //分享
    // 上拉加载分页scroll-view到底部加载更多
    onreachBottom() {
      console.log('2')
      this.GetMyGoodsListDataList(1)
      var pagezs = (this.pagecont + 10 - 1) / 10
      if (this.pageindex >= pagezs) {
        this.status = 'nomore'
        return
      }
      this.status = 'loadmore'
      setTimeout(() => {
        if (this.pageindex >= pagezs) {
          this.status = 'nomore'
        } else {
          this.status = 'loading'
        }
      }, 1000)
    },
    // 车源管理接口
    async GetMyGoodsListDataList(type) {
      // if (this.swiperCurrent !== 0) {
      uni.showLoading({
        title: '加载中',
      })
      // }
      if (type === 0) {
        this.pageindex = 1
      } else if (type === 1) {
        this.pageindex = ++this.pageindex
      }
      try {
        let data = {
          'GoodsState': this.swiperCurrent, //商品状态（0全部，1出售中，2已下架，3已过期）
          'Type': this.stateTextid, //分类（0全部，1渠道-对应商户端，2零售-对应消费端，3对应客户端和销售端）
          'PageIndex': this.pageindex,
          'PageSize': 10,
          'Store_Id': this.$store.state.userInfoDetail.store.storeId
        }
        const res = await this.$api.carSource.GetMyGoodsListDataList(data)
        this.pagecont = res.data.total
        this.showPage = true //显示loading
        if (res.data.list === null && this.pageindex === 1) {
          this.pageList = []
          return false
        }
        uni.hideLoading()
        // 为空显示默认logo
        if (res.data.list.length === 0) {
          this.pagelistull = true
        } else {
          this.pagelistull = false
        }

        if (this.pageindex === 1) {
          this.pageList = res.data.list
        } else if (this.pageindex > 1) {
          this.pageList = this.pageList.concat(res.data.list)
        }
        //商品状态（0全部，1出售中，2已下架，3已过期）
        switch (this.swiperCurrent) {
        case 0:
          this.defaultMapTitle = '暂无车源~快去发布吧'
          break
        case 1:
          this.defaultMapTitle = '暂无车源~快去发布吧'
          break
        case 2:
          this.defaultMapTitle = '暂无下架车源'
          break
        case 3:
          this.defaultMapTitle = '暂无过期车源'
          break
        }
      } catch (err) {
        console.log(err)
      }
    },
    // tabs通知swiper切换
    tabsChange(index) {
      var e = {
        detail: {
          current: index
        }
      }
      this.swiperCurrent = index
    },
    // swiper-item左右移动，通知tabs的滑块跟随移动
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    // 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
    // swiper滑动结束，分别设置tabs和swiper的状态
    animationfinish(e) {
      let current = e.detail.current
      this.$refs.uTabs.setFinishCurrent(current)
      this.swiperCurrent = current
      // console.log('23423', this.swiperCurrent)
      this.current = current
      // alert('滑动4')//改变时会触发 change 事件
      this.GetMyGoodsListDataList(0)
    },
    // 点击分类
    fenClass(tylist) {
      this.show = false
      this.stateText = tylist.name
      this.stateTextid = tylist.id
      console.log('5')
      this.GetMyGoodsListDataList(0)
    },

    //商户端有效期
    validPeriodClick(e) {
      this.Production = ''
      if (this.modesc === 'time') {
        if (this.paramssc.year) this.Production += e.year
        if (this.paramssc.month) this.Production += '-' + e.month
        if (this.paramssc.day) this.Production += '-' + e.day
        if (this.paramssc.hour) this.Production += ' ' + e.day
        if (this.paramssc.minute) this.Production += ':' + e.minute
        if (this.paramssc.second) this.Production += ':' + e.second
      } else {
        this.Production = e.year + '-' + e.month + '-' + e.day
      }
      this.showsjrq = false
      this.validPeriod = this.Production
      this.UpdateMyCarSourceUpload()
    },
    //商户端有效期关闭
    closevalidPeriod() {
      this.showsjrq = false
    },
    fenlei() {
      this.show = !this.show
    },
    //批量管理
    gobatch() {
      uni.navigateTo({
        url: '/pages_carSource/theBatchManagement/theBatchManagement?swiperCurrent=' + this.swiperCurrent +
          '&stateTextid=' + this.stateTextid
      })
    },
    //发布车源
    goYv2() {
      uni.navigateTo({
        url: '/pages_carSource/releaseOptions/releaseOptions?type=1'
      })
    },
    //编辑车源
    goYedit(types) {
      uni.navigateTo({
        url: '/pages_carSource/releaseOptions/releaseOptions?goodsid=' + types.id + '&&type=110' + '&sheelistid=' +
          types.shelve_Status
      })
    },
    // 跳转详情
    preview(items) {
      // if (items.shelve_Status === 1) {
      //   this.$refs.uToast.show({
      //     title: '此车已下架',
      //     type: 'warning',
      //     url: '/pages/user/index'
      //   })
      //   return
      // }
      uni.navigateTo({
        url: '/pages_carSource/productDetail/productDetail?goodsid=' + items.id
      })
      // if(this.stateText == '商户端') {
      // 	uni.navigateTo({
      // 		url:'/pages_Sxy/Sxy_productDetail/Sxy_productDetail'
      // 	})
      // }
    },
    //上架
    shagnjiaClick(types) {
      this.showsjrq = true
      this.showShangjiaId = types.id
    },
    // 下架
    xiajiaClick(types) {
      this.showXiajia = true
      this.showXiajiaId = types.id
    },
    //确认下架
    footerBtn() {
      this.UpdateMyCarSourceShelves()
      this.showXiajia = false
    },
    // 下架接口
    async UpdateMyCarSourceShelves() {
      try {
        let data = {
          'Type': 0, //0下架，1删除
          'Id': [
            this.showXiajiaId
          ],
        }
        const res = await this.$api.carSource.UpdateMyCarSourceShelves(data)
        console.log(res)
        if (res.code === 200) {
          this.$refs.uToast.show({
            title: '下架成功',
            type: 'success',
            url: '/pages/user/index'
          })
          console.log('6')
          this.GetMyGoodsListDataList(0)
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 长期有效
    valclick(index) {
      this.changqili.forEach((item, i) => {
        item.check = item.check === false ? true : false
      })
      if (index === 0) {
        this.validPeriod = '2029-1-1'
        this.showsjrq = false
        this.UpdateMyCarSourceUpload()
      }
    },
    // 上架接口
    async UpdateMyCarSourceUpload() {
      try {
        let data = {
          'Id': this.showShangjiaId,
          'Valid_Time': this.validPeriod
        }
        const res = await this.$api.carSource.UpdateMyCarSourceUpload(data)
        console.log(res)
        if (res.code === 200) {
          this.$refs.uToast.show({
            title: '上架成功',
            type: 'success',
            url: '/pages/user/index'
          })
          console.log('7')
          this.GetMyGoodsListDataList(0)
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 获取海报图片列表
    async getPosters() {
      try {
        const {
          data
        } = await this.$api.jsonData.getPosters()
        // 为每个海报图片列表添加对应海报的type类型标识
        this.onegood = data.onegood.map(d => ({ ...d,
          type: 'onegood'
        }))
      } catch (err) {
        //
      }
    },
    //分享跳转
    fenx(itemData) {
      this.$refs.uToast.show({
        title: '敬请期待',
        icon: false,
        type: 'warning',
      })
      // let data = {
      //   bus_Price: itemData.bus_Price, //售价
      //   car_Type_Name: itemData.car_Type_Name, //汽车系列名称
      //   checked: false,
      //   configuration_Name: itemData.configuration_Name, //车子配置名称
      //   difference: itemData.difference, //差价（涨价价格前加 - 如：-1.5万为涨价1.5万）
      //   factory_Price: itemData.factory_Price, //官方指导价
      //   goodImg: itemData.goodimg, //商品主图
      //   id: itemData.id, //id
      // }
      // this.setPosterBgTemplate(this.onegood[0]) //海报图片list
      // this.setGoodsData([data]) //商品信息
      // this.haibaotrue = true //显示弹框
    },
  }
}
